/***************************************************************************
*  Udpflea.hpp
*
*  July 2007
*  Copyright  2007  RuijinZheng,China
*  Email mebetop@126.com
****************************************************************************/

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#ifndef UDPFLEALAYER_HPP
#define UDPFLEALAYER_HPP
#include "Lowlayer.hpp"
#include <list>
#include <map>
#define MAXBINDATTEMPT 20
typedef int SocketType;
typedef pair<const SocketType,Udpproxy *> Pair;
typedef map<SocketType,Udpproxy *> Socktoudpflea;
class Udpflealayer:public Lowlayer
{
public:

       virtual ~Udpflealayer();

       virtual int Connectflea(const int &sockfd,const  sockaddr *serv_addr,
                  const vector<Proxyserver> &availableserver) { return 0;};
       static Udpflealayer *Getonlyinstance(const old_bind *origbind,const old_connect *origconnect,
                 const old_recv *origrecv, const old_send *origsend,const old_recvfrom *origrecvfrom,
                 const old_sendto *origsendto);

       virtual const Proxyserver *Getproxyserver(const vector<Proxyserver> &availableserver);
       virtual  ssize_t Sendtoflea(int sockfd,const void* buff,size_t buflen,int flags,
               const sockaddr  *to,socklen_t  tolen,const Udpproxy *responsestruct);
       virtual ssize_t Recvfromflea(int sockfd,void *buff,size_t buflen,int flags,sockaddr *from,
               socklen_t *fromlen);
       Udpproxy * Initialresponseaddr(const int &sock,const SocketType & tcpsock,const struct sockaddr *addr);
       SocketType Begintcpcommunicate(const vector<Proxyserver> &availableserver);
       sockaddr_in * Getbindedaddr(const int &sock);
       Udpproxy * GetUdplayerReady(int & sock,const struct sockaddr *addr,const vector<Proxyserver> &availableserver);
       Udpproxy *Getreadyresponseaddr(SocketType &sock);

protected:

       Udpflealayer(const old_bind *origbind,const old_connect *origconnect,const old_recv *origrecv,
               const old_send *origsend,const old_recvfrom *origrecvfrom,const old_sendto *origsendto);

       

private:

       int getramdomport() const;

private:

       pthread_mutex_t mut;
       Socktoudpflea udpflealinks;
       Udpproxy *proxy;
       old_bind oldbind;
       SocketType tcpsock;
       int maxport;
       int minport;

};
#endif
